from rest_framework.viewsets import ModelViewSet
from meiduo_admin.utils import PageNum, PageNum1
from goods.models import SPU, Brand, GoodsCategory
from meiduo_admin.serializers.spus import SPUSerializer, BrandSerializer,GoodsCategorySerializer
from rest_framework.response import Response


class SPUViewSet(ModelViewSet):
    '''
    SPU表的增删改查
    '''
    serializer_class = SPUSerializer
    queryset = SPU.objects.all()
    pagination_class = PageNum1

    def brand(self, request):
        data = Brand.objects.all()
        ser = BrandSerializer(data, many=True)
        return Response(ser.data)

    def channel(self,request):
        data=GoodsCategory.objects.filter(parent=None)
        ser=GoodsCategorySerializer(data,many=True)
        return Response(ser.data)

    def channel2(self,request,pk):
        data=GoodsCategory.objects.filter(parent_id=pk)
        ser=GoodsCategorySerializer(data,many=True)
        return Response({'subs':ser.data})

